package com.shu.qq.common;

import java.io.Serializable;

/**
 * 统一结果返回类
 *
 * @author SouthMaple
 * @since 2021-04-07 20:31:51
 */
public class R<T> implements Serializable {
    public static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    private int code;

    /**
     * 数据
     */
    private T data;

    /**
     * 信息
     */
    private String msg;

    public R(Common common) {
        this(common, null);
    }

    public R(int code, String msg) {
        this(code, msg, null);
    }

    public R(int code, String msg, T data) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    public R(Common common, T data) {
        this.code = common.getCode();
        this.msg = common.getMsg();
        this.data = data;
    }


    public static R SUCCESS() {
        return new R(Common.SUCCESS);
    }

    public static R SUCCESS(String msg) {
        return new R(200, msg);
    }

    public static R FAILED() {
        return new R(Common.FAILED);
    }

    public static R FAILED(String msg) {
        return new R(400, msg);
    }

    public static R ERROR() {
        return new R(Common.ERROR);
    }

    public static R ERROR(String msg) {
        return new R(500, msg);
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    enum Common {
        SUCCESS(200, "执行成功"),
        FAILED(400, "执行失败"),
        ERROR(500, "执行异常");

        private int code;
        private String msg;

        private Common(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }
    }

}

